//
//  MNCacheClass.m
//  XDance
//
//  Created by Vill on 2023/9/18.
//

#import "MNCacheClass.h"
#import "NSObject+YYModel.h"

@implementation MNCacheClass

///数据持久化保存的key
+ (void)mn_saveModel:(id)model key:(NSString *)key{
    //model转json
    NSDictionary *productDic = [model yy_modelToJSONObject];
    /**
     NSUserDefaults支持的数据格式有：NSNumber（Integer、Float、Double），NSString，NSDate，NSArray，NSDictionary，BOOL类型，而我们这里存储的是自定义的model类型-ProductModel，该类型不被支持
     */
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:productDic];
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:key];
}

///获取数据持久化保存的Model  - 要传入model的Class - 内部进行yyModel转模型
+ (id) mn_getSaveModelWithkey:(NSString *)key modelClass:(Class)modelClass{
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    NSDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    return [modelClass yy_modelWithJSON:dict];
}

///获取保存的模型数组
+ (id) mn_getSaveModelArrayWithkey:(NSString *)key modelClass:(Class)modelClass{
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    return [NSArray yy_modelArrayWithClass:[modelClass class] json:array];
}


@end
